package me.spring.cloud.components.starter.lock.configuration;

import me.spring.cloud.common.components.lock.annotation.DistributedLockAspect;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import redis.clients.jedis.JedisPool;

/**
 * @author: luffy
 * @create: 2018-09-12 12:59
 **/
@Configuration
@Import(DistributedLockAspect.class)
public class RedisLockConfiguration {

  @Autowired
  private RedisProperties redisProperties;

  @Bean
  public JedisPool jedisPool() {
    GenericObjectPoolConfig gop = new GenericObjectPoolConfig();
    if(null != redisProperties.getJedis() && null != redisProperties.getJedis().getPool()) {
      gop.setMaxIdle(redisProperties.getJedis().getPool().getMaxIdle());
      gop.setMaxTotal(redisProperties.getJedis().getPool().getMaxActive());
      gop.setMinIdle(redisProperties.getJedis().getPool().getMinIdle());
    }
    return new JedisPool(gop, redisProperties.getHost(), redisProperties.getPort(),
        null != redisProperties.getTimeout() ? (int) redisProperties.getTimeout().getSeconds() : 0,
        redisProperties.getPassword(), redisProperties.getDatabase());
  }
}
